/*
 * Copyright (C) 2007-2015 FBReader.ORG Limited <contact@fbreader.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.fbreader.bookmodel;

import com.example.administrator.fangbook.appliction.HtlApplication;

import org.geometerplus.android.fbreader.SQLiteBooksDatabase;
import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.fbreader.book.M17kPlainTxtBook;
import org.geometerplus.fbreader.book.ReadBook;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.zlibrary.core.fonts.FileInfo;
import org.geometerplus.zlibrary.core.fonts.FontEntry;
import org.geometerplus.zlibrary.core.fonts.FontManager;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.text.model.CachedCharStorage;
import org.geometerplus.zlibrary.text.model.CachedCharStorageException;
import org.geometerplus.zlibrary.text.model.ZLTextModel;
import org.geometerplus.zlibrary.text.model.ZLTextPlainModel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public final class BookModel {

    public ReadBook Book;

//    public TOCTree TOCTree = new TOCTree();
//    public FontManager FontManager = new FontManager();
//
//    protected CachedCharStorage myInternalHyperlinks;
//    protected HashMap<String, ZLImage> myImageMap = new HashMap<String, ZLImage>();
//    protected ZLTextModel myBookTextModel;
//
//    protected HashMap<String, ZLTextModel> myFootnotes = new HashMap<String, ZLTextModel>();
//    protected List<Bookmark> mAllBookmarks = Collections.synchronizedList(new ArrayList<Bookmark>());
//    protected List<BookReadNote> mAllNotes = Collections.synchronizedList(new LinkedList<BookReadNote>());
//    protected List<LocalDumpNote> mAllLocalUnderLineBlocks = Collections.synchronizedList(new LinkedList<LocalDumpNote>());

    //protected OnSetBookTextModel mSetBookTextModel = null;
//    public static final class Label {
//        public final String ModelId;
//        public final int ParagraphIndex;
//
//        public Label(String modelId, int paragraphIndex) {
//            ModelId = modelId;
//            ParagraphIndex = paragraphIndex;
//        }
//    }

    public BookModel(ReadBook book) {
        Book = book;
        //数据库加载书签
//        loadBookmarks(null);
//        //加载读书笔记
//        loadBooknotes();
//        if (Book instanceof M17kPlainTxtBook) {
//            M17kPlainTxtBook _17kBook = (M17kPlainTxtBook) Book;
//            mAllLocalUnderLineBlocks.clear();
//            try {
//                mAllLocalUnderLineBlocks.addAll(HtlApplication.getInstance().getDataHelper().getLocalUnderLineBlockseDao().queryForEq("bookId", _17kBook.getBookId()));
//            } catch (SQLException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
    }

//    public void loadBookmarks(String tag) {
//        mAllBookmarks.clear();
//        mAllBookmarks.addAll(SQLiteBooksDatabase.Instance().loadBookmarks(Book.getId(), true, tag));
//        LogUtil.d("ZLLogger", "load bookmarks:" + tag + " marks count:" + mAllBookmarks.size());
//
//    }
//
//    public List<Bookmark> getAllBookmarks() {
//        LogUtil.d("ZLLogger", "all bookmarks size:" + mAllBookmarks.size());
//        return mAllBookmarks;
//    }
//
//    public List<BookReadNote> getAllNotes() {
//        return mAllNotes;
//    }
//
//    private void loadBooknotes() {
//        if (Book instanceof M17kPlainTxtBook) {
//            M17kPlainTxtBook _17kBook = (M17kPlainTxtBook) Book;
//            mAllNotes.clear();
//            try {
//                mAllNotes.addAll(GlobalApp.getInstance().getDataHelper().getBookReadNoteDao().queryForEq("bookId", _17kBook.getBookId()));
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    public void setBookNotes(List<BookReadNote> notes) {
//        synchronized (mAllNotes) {
//            if (Book instanceof M17kPlainTxtBook) {
//                mAllNotes.clear();
//                if (notes != null) {
//                    mAllNotes.addAll(notes);
//                }
//            }
//        }
//    }
//
//    public List<LocalDumpNote> getAllLocalUnderLineBlocks() {
//
//        return mAllLocalUnderLineBlocks;
//    }
//
//    public interface LabelResolver {
//        List<String> getCandidates(String id);
//    }
//
//    private LabelResolver myResolver;
//
//    public void setLabelResolver(LabelResolver resolver) {
//        myResolver = resolver;
//    }
//
//    public Label getLabel(String id) {
//        Label label = getLabelInternal(id);
//        if (label == null && myResolver != null) {
//            for (String candidate : myResolver.getCandidates(id)) {
//                label = getLabelInternal(candidate);
//                if (label != null) {
//                    break;
//                }
//            }
//        }
//        return label;
//    }
//
//    public void registerFontFamilyList(String[] families) {
//        FontManager.index(Arrays.asList(families));
//    }
//
//    public void registerFontEntry(String family, FontEntry entry) {
//        FontManager.Entries.put(family, entry);
//    }
//
//    public void registerFontEntry(String family, FileInfo normal, FileInfo bold, FileInfo italic, FileInfo boldItalic) {
//        registerFontEntry(family, new FontEntry(family, normal, bold, italic, boldItalic));
//    }
//
//    public ZLTextModel createTextModel(
//            String id, String language, int paragraphsNumber,
//            int[] entryIndices, int[] entryOffsets,
//            int[] paragraphLenghts, int[] textSizes, byte[] paragraphKinds,
//            String directoryName, String fileExtension, int blocksNumber
//    ) {
//        return new ZLTextPlainModel(
//                id, language, paragraphsNumber,
//                entryIndices, entryOffsets,
//                paragraphLenghts, textSizes, paragraphKinds,
//                directoryName, fileExtension, blocksNumber, myImageMap, FontManager
//        );
//    }
//
//
//    public void setLoadErrorState(String errTipMsg) {
//        FBReaderApp reader = (FBReaderApp) FBReaderApp.Instance();
//        reader.BookTextView.initErrorInfo(errTipMsg);
//    }
//
//
//    public void setFootnoteModel(ZLTextModel model) {
//
//        //LogUtil.e("ZLLogger", "setBookTextModel");
//        myFootnotes.put(model.getId(), model);
//    }
//
//    public ZLTextModel getTextModel() {
//        return myBookTextModel;
//    }
//
//    public ZLTextModel getFootnoteModel(String id) {
//        return myFootnotes.get(id);
//    }
//
//    public void addImage(String id, ZLImage image) {
//        myImageMap.put(id, image);
//    }
//
//    public void initInternalHyperlinks(String directoryName, String fileExtension, int blocksNumber) {
//        myInternalHyperlinks = new CachedCharStorage(directoryName, fileExtension, blocksNumber);
//    }
//
//    private org.geometerplus.fbreader.bookmodel.TOCTree myCurrentTree = TOCTree;
//
//    public void addTOCItem(String text, int reference) {
//        myCurrentTree = new TOCTree(myCurrentTree);
//        myCurrentTree.setText(text);
//        myCurrentTree.setReference(myBookTextModel, reference);
//    }
//
//    public void leaveTOCItem() {
//        myCurrentTree = myCurrentTree.Parent;
//        if (myCurrentTree == null) {
//            myCurrentTree = TOCTree;
//        }
//    }
//
//    private Label getLabelInternal(String id) {
//        final int len = id.length();
//        final int size = myInternalHyperlinks.size();
//        //fixed:4.3.0崩溃日志收集， myStorage.block 可能会抛出CachedCharStorageException
//        try {
//            for (int i = 0; i < size; ++i) {
//                final char[] block = myInternalHyperlinks.block(i);
//                for (int offset = 0; offset < block.length; ) {
//                    final int labelLength = (int) block[offset++];
//                    if (labelLength == 0) {
//                        break;
//                    }
//                    final int idLength = (int) block[offset + labelLength];
//                    if (labelLength != len || !id.equals(new String(block, offset, labelLength))) {
//                        offset += labelLength + idLength + 3;
//                        continue;
//                    }
//                    offset += labelLength + 1;
//                    final String modelId = (idLength > 0) ? new String(block, offset, idLength) : null;
//                    offset += idLength;
//                    final int paragraphNumber = (int) block[offset] + (((int) block[offset + 1]) << 16);
//                    return new Label(modelId, paragraphNumber);
//                }
//            }
//        } catch (CachedCharStorageException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public void reset() {
//
////        myInternalHyperlinks = new CachedCharStorage(32768, Paths.cacheDirectory(), "links");
////        BookTextModel = new ZLTextWritablePlainModel(null, Book.getLanguage(), 8, 512, Paths.cacheDirectory(), "cache" + myFootnotes.size(), myImageMap);
//    }
//
//    public boolean mLoaded = true;
//
//    public boolean isLoaded() {
//        return mLoaded;
//    }
//
//    public void destroy() {
//        myResolver = null;
//        if (Book != null) {
//            Book.setBookReader(null);
//        }
//        Book = null;
//        if (TOCTree != null) {
//            TOCTree.clear();
//        }
//        TOCTree = null;
//        FontManager = null;
//        myInternalHyperlinks = null;
//        if (myImageMap != null) {
//            myImageMap.clear();
//        }
//        myImageMap = null;
//        if (myBookTextModel != null) {
//            myBookTextModel.destroy();
//        }
//        myBookTextModel = null;
//
//        if (myFootnotes != null) {
//            myFootnotes.clear();
//        }
//        myFootnotes = null;
//        if (mAllBookmarks != null) {
//            mAllBookmarks.clear();
//        }
//        mAllBookmarks = null;
//        if (mAllNotes != null) {
//            mAllNotes.clear();
//        }
//        mAllNotes = null;
//        if (mAllLocalUnderLineBlocks != null) {
//            mAllLocalUnderLineBlocks.clear();
//        }
//        mAllLocalUnderLineBlocks = null;
//    }
}
